<?

/** Printing  related classes and functions.
 *  Note! This uses soPdf and therefore requires pdflib support php.
 *
 * This collection of functions (libary) is licensed under GPL2.
 * See LICENSE or www.gnu.org for more details.
 */

include_once("sostring.php");
include_once("sopdf.php");


/** Document printing class
 */
class soPrint {
    var $__PageSize = "";     /** str "a4", etc */
    var $__Encoding = "";     /** str "host", "buildin", "winansi" or
                               *  "macroman" */
    var $__DocOpen = 0;       /** bool Flag to indicate if doc is started but
                               *  not commited/cancelled yet */
    var $__PageOpen = 0;      /** bool Flag to indicate if doc has a page that
                               *  has not been commited yet */
    var $__LeftMargin;        /** Left margin in points */
    var $__TopMargin;         /** Top margin in points */
    var $__Pdf;               /** soPdf Pdf object */


    /** Constructor
     *  >$Template:str soStream source
     *  >$Unit:str Unit (mm, cm, m...) to use in dimensions and positions.
     */
    function soPrint($Template, $Unit = "mm") {
        $this->__Template = new soInputStream($Template);
        $t_unit = $this->__Template->getValue("sys", "unit");
        $this->__Pdf = new soPdf($Unit);
    } // soPrint


    /** Starts a new document
     *  >$Left:int Left margin
     *  >$Top:int Top margin
     *  >$PageSize:str Page size code: "a4"...
     *  <:bool Returns true(1) if parameters are correct or false(0) if not.
     */
    function beginDoc($Left, $Top, $PageSize = "a4", $Encoding = "host") {
        $this->__Pdf->beginDoc($Left, $Top, $PageSize, $Encoding);

        $this->__DocOpen = 0;
        $this->beginPage();
    } // soPdf


    /** Closes document.
     *  Common code for cancel/commitDoc
      */
    function __endDoc() {
        $this->endPage();
        $this->__DocOpen = 0;
    } // __endDoc


    /** Commits and print a document.
     *  >$Output:str Target process/file to write to
     */
    function commitDoc($Output) {
        $this->__endDoc();

        $ph = fopen($Output, "w");
        fputs($ph, pdf_get_buffer($this->__Handle));
        fclose($ph);
    } // commitDoc


    /** Cancels and resets a document
     */
    function cancelDoc() {
        $this->__endDoc();
    } // commitDoc


    /** Start a new page in the document.
     */
    function beginPage() {
        if ($this->__PageOpen) $this->closePage();
        $this->__PageOpen = 1;
        pdf_begin_page($this->__Handle, $this->getPageWidth(),
                       $this->getPageHeight());
    } // beginPage


    /** Ends an open page with beginPage in the document.
     */
    function endPage() {
        if ($this->__PageOpen) pdf_end_page($this->__Handle);
        $this->__PageOpen = 0;
    } // beginPage


    /** Returns current page width.
     */
    function getPageWidth() {
        return $this->__PageDims[$this->__PageSize][0];
    } // getPageWidth


    /** Returns current page width.
     */
    function getPageHeight() {
        return $this->__PageDims[$this->__PageSize][1];
    } // getPageWidth


    /** Prints aligned text at x,y
     *  >$X:dec Points from left
     *  >$Y:dec Points from top
     *  >$WIdth:dec Text area box width
     *  >$height:dec Text area box height
     *  >$Align:SOTA_* Text alignment
     *  >$Txt:str Text to print
     */
    function printXYW($X, $Y, $Width, $Height, $Align, $Txt) {
        $X = $this->__UnitPoints * $X;
        $Y = $this->__UnitPoints * $Y;
        pdf_show_boxed($this->__Handle, $Txt, round($this->__LeftMargin +$X),
                       round($this->__getBottomY($Y)),
                       $Width, $Height, $Align);
    } // printXYW


    /** Returns bottom related Y value for Top related Y value with margin
     *  correction.
     *  >$$Y:dec Top related Y value
     *  <:dev Returns bottom related Y value
     */
    function __getBottomY($Y) {
        return $this->getPageHeight() -($this->__TopMargin +$Y);
    }


    /** Sets current font to use
     *  >$Name:str Font name to use
     *  >$Size:int Font size in points
     *  <:bool Returns true(1) when font was set successfully, or false(0) on
     *      error
     */
    function setFont($Name, $Size) {
/*        pdf_set_parameter($this->__Handle, "resourcefile", FONTS_FILE);
        $font = pdf_findfont($this->__Handle, $Name,
                             $this->__Encoding, 1);
        if ($font) pdf_setfont($this->__Handle, $font,  $Size);

        return ($font != 0);
*/
        pdf_set_font($this->__Handle, $Name, $Size, "host");
        return 1;
    } // setFont


    /** Prints a line from a to b.
     *  >$Width:dec Thickness of the line
     *  >$X1:dec Start X point
     *  >$Y1:dec Start Y point
     *  >$X2:dec End X point
     *  >$Y2:dec End Y point
     */
    function printLine($Width, $X1, $Y1, $X2, $Y2) {
        $X1 = $this->__UnitPoints * $X1;
        $Y1 = $this->__UnitPoints * $Y1;
        $X2 = $this->__UnitPoints * $X2;
        $Y2 = $this->__UnitPoints * $Y2;
        pdf_setlinewidth($this->__Handle, $Width *$this->__UnitPoints);
        pdf_moveto($this->__Handle, $this->__LeftMargin +$X1,
                   round($this->__getBottomY($Y1)));
        pdf_lineto($this->__Handle, $this->__LeftMargin +$X2,
                   round($this->__getBottomY($Y2)));
        pdf_stroke($this->__Handle);
    } //


    /** Prints an image from a file.
     *  >$Image:str File name.
     *  >$X:dec Image left pos
     *  >$Y:dec Image top pos
     $  >$Scale:dec Scaling factor 1=normal
     *  <:bool Returns true(1) if file was found and it's type recogniced,
     *  otherwise false(0).
     */
    function printImage($Name, $X, $Y, $Scale = 1) {
        $itype = strtolower(soExtractVal($Name, ".", -1));
        switch ($itype) {
            case "jpg":
                $itype = "jpeg";
                break;
            case "tif":
                $itype = "tiff";
                break;
        }; // switch

        $image = pdf_open_image_file($this->__Handle, $itype, $Name);
        if ($image) {
            $iheight = pdf_get_image_height($this->__Handle, $image) *$Scale;
            $X = $this->__UnitPoints *$X;
            $Y = $this->__UnitPoints *$Y;
            pdf_place_image($this->__Handle, $image,
                            round($this->__LeftMargin +$X),
                            round($this->__getBottomY($Y) -$iheight),
                            $Scale);
            pdf_close_image($this->__Handle, $image);
        }; // if image
        return ($image != 0);
    } // printImage

}; // soPrint


/*

Test code - can be removed anytime soon :-)

$x = new soPdf();
$x->beginDoc(10, 10, "a4", "host");
$x->printImage("/usr/lib/YaST2/images/start-tux.png", 0, 0, 0.5);
$x->printLine(0.1, 100, 100, 100, 200);
$x->setFont("Helvetica", 10);
$x->printXYW(10, 10, 100, 20, SOTA_LEFT, "Tikkerperi");
$x->printXYW(10, 20, 100, 20, SOTA_RIGHT, "Tikkerperi");
$x->setFont("Helvetica-Bold", 10);
$x->printXYW(10, 40, 100, 20, SOTA_LEFT, "Tikkerperi");
$x->commitDoc("testi.pdf");
*/

?>
